/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.pro.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.common.persistence.ResponseResult;
import com.thinkgem.jeesite.modules.pro.entity.BasBuilding;
import com.thinkgem.jeesite.modules.pro.service.BasBuildingService;
import com.thinkgem.jeesite.modules.sys.entity.Dict;
import com.thinkgem.jeesite.modules.sys.utils.DictUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.pro.entity.BasPerson;
import com.thinkgem.jeesite.modules.pro.service.BasPersonService;

import java.util.List;

/**
 * 业主管理Controller
 * @author dby
 * @version 2020-10-15
 */
@Controller
@RequestMapping(value = "${adminPath}/pro/basPerson")
public class BasPersonController extends BaseController {

	@Autowired
	private BasPersonService basPersonService;

	@Autowired
	private BasBuildingService basBuildingService;
	
	@ModelAttribute
	public BasPerson get(@RequestParam(required=false) String id) {
		BasPerson entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = basPersonService.get(id);
		}
		if (entity == null){
			entity = new BasPerson();
		}
		return entity;
	}
	
	@RequiresPermissions("pro:basPerson:view")
	@RequestMapping(value = {"list", ""})
	public String list(BasPerson basPerson, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<BasPerson> page = basPersonService.findPage(new Page<BasPerson>(request, response), basPerson); 
		model.addAttribute("page", page);
		return "modules/pro/basPersonList";
	}

	@RequiresPermissions("pro:basPerson:view")
	@RequestMapping(value = "form")
	public String form(BasPerson basPerson, Model model) {
		model.addAttribute("basPerson", basPerson);
		List<Dict> list = DictUtils.getDictList("interest");

		List<BasBuilding> buildingList =basBuildingService.findSelectForCorridor();
		model.addAttribute("buildingList",buildingList);
		model.addAttribute("list" ,list);
		return "modules/pro/basPersonForm";
	}

	@RequiresPermissions("pro:basPerson:edit")
	@RequestMapping(value = "save")
	@ResponseBody
	public ResponseResult save(BasPerson basPerson, Model model, RedirectAttributes redirectAttributes) {

		if(null==basPerson || StringUtils.isBlank(basPerson.getCorridor())){
			return ResponseResult.failure("所在楼道不能为空！");
		}
		int count = basPersonService.findCorridor(basPerson);
		if(count>0){
			return ResponseResult.failure("所在楼道已存在");
		}

		if(null==basPerson || StringUtils.isBlank(basPerson.getIdCard())){
			return ResponseResult.failure("身份证号码不能为空！");
		}
		int count2 = basPersonService.findIdCard(basPerson);
		if(count2>0){
			return ResponseResult.failure("身份证号码已存在");
		}
		basPersonService.save(basPerson);
		return ResponseResult.success();
	}
	
	@RequiresPermissions("pro:basPerson:edit")
	@RequestMapping(value = "delete")
	public String delete(BasPerson basPerson, RedirectAttributes redirectAttributes) {
		basPersonService.delete(basPerson);
		addMessage(redirectAttributes, "删除业主管理成功");
		return "redirect:"+Global.getAdminPath()+"/pro/basPerson/?repage";
	}

	@RequiresPermissions("pro:basPerson:view")
	@RequestMapping(value = "view")
	public String view(BasPerson basPerson, Model model) {
		model.addAttribute("basPerson", basPerson);
		return "modules/pro/basPersonView";
	}

}